<?php 

class paomeHomeController extends base
{
    public function __construct()
    {
        parent::__construct('show');
        
        $this->printResult = TRUE;
        $this->in_user();
    }

    /**
    * 首页
    */
    public function dopaomeHome()
    {
        // 获取传入参数
    	$uid = $this->uid ? $this->uid : 0;
        /*if(empty($uid))
        {
          $_ENV['error']->getErrors(201);
          exit;
        }*/
        $homeInfo = array();
        $homeInfo['isJoin'] = 0;    //是否参与
        $homeInfo['clubStatus'] = 0; //战队竞赛状态(0：赛季未开始)
        $homeInfo['clubStatusNew'] = 0; //战队竞赛状态(0：赛季未开始)
        $clubInfo = array();
        $recommentClub = array();
        //获取当前赛季状态
        $season = $this->load('paome/paomeHall')->getCurrentSeason();
        //查看俱乐部用户加入状态
        $userClubStatus = $this->load("paome/home")->userClubStatus($uid);
        if(!empty($userClubStatus)){
            if($userClubStatus['status'] == -1){
                $homeInfo['isJoin'] = 1;   //待审核
            }else{
                $homeInfo['isJoin'] = 2;   //已加入
                //俱乐部信息
                $clubInfo = $this->load("paome/paomeHall")->getClubByUid($uid);
                //是否是团长
                $clubInfo['isLeader'] = 0;
                if($uid == $clubInfo['head_uid']){
                    $clubInfo['isLeader'] = 1;
                }
                //查看是否晋级冠军赛
                if(!empty($season)){
                    if($season['status'] == 1){
                        //招募期
                        $homeInfo['clubStatus'] = 1;
                        //距预选赛开始时间
                        $homeInfo['countdown'] = ceil($season['start_time'] - time()/86400);
                    }else{
                       //查看俱乐部当前状态
                        $clubStatus = $this->load('paome/center')->getClubStatus($season['id'], $clubInfo['id']);
                        if($clubStatus){
                            if($clubStatus['status'] == 5 || $clubStatus['status'] == 10){
                                $homeInfo['clubStatus'] = 2;    //已淘汰
                            }
                            if($clubStatus['status'] == 20){
                                $homeInfo['clubStatus'] = 3;    //已结束
                                //查看是否具有争霸赛资格
                                $clubChampions = $this->load('paome/home')->clubChampions($season['id'], $clubInfo['id']);
                                //$clubInfo['isChampions'] = 1;
                            }
                            if($clubStatus['status'] == 15){
                                $homeInfo['clubStatus'] = 4;    //进行中
                            }
                        } 
                    }
                }
                //新的状态判断
                if(!empty($season)){
                    $clubStatusNew = $this->load('paome/center')->getClubStatus($season['id'], $clubInfo['id']);
                    if($clubStatusNew){
                        if($clubStatusNew['type'] == 1){   //第一轮预选赛
                            //第一轮预选赛招募期(当前战队无状态)
                            $clubStatusNew['status'] == 0 && $homeInfo['clubStatusNew'] = 1; 
                            //第一轮预选赛已淘汰(人数未满下限)   
                            $clubStatusNew['status'] == 5 && $homeInfo['clubStatusNew'] = 2;  
                            //第一轮预选赛进行中   
                            $clubStatusNew['status'] == 15 && $homeInfo['clubStatusNew'] = 3;
                            //第一轮预选赛空档期   
                            $clubStatusNew['status'] == 20 && $homeInfo['clubStatusNew'] = 4;
                            //第一轮预选赛已淘汰(未能晋级冠军赛)   
                            $clubStatusNew['status'] == 10 && $homeInfo['clubStatusNew'] = 5;    
                        }
                        if($clubStatusNew['type'] == 5){    //第一轮决赛
                            //第一轮冠军赛进行中   
                            $clubStatusNew['status'] == 15 && $homeInfo['clubStatusNew'] = 6;
                            //第一轮冠军赛空档期   
                            $clubStatusNew['status'] == 20 && $homeInfo['clubStatusNew'] = 7;
                            //第一轮冠军赛已淘汰(未能晋级争霸赛)   
                            $clubStatusNew['status'] == 10 && $homeInfo['clubStatusNew'] = 8;   
                        }
                        if($clubStatusNew['type'] == 10){    //第二轮预选赛
                            //第二轮预选赛招募期(当前战队无状态)
                            $clubStatusNew['status'] == 0 && $homeInfo['clubStatusNew'] = 9; 
                            //第二轮预选赛已淘汰(人数未满下限)   
                            $clubStatusNew['status'] == 5 && $homeInfo['clubStatusNew'] = 10;  
                            //第二轮预选赛进行中   
                            $clubStatusNew['status'] == 15 && $homeInfo['clubStatusNew'] = 11;
                            //第二轮预选赛空档期   
                            $clubStatusNew['status'] == 20 && $homeInfo['clubStatusNew'] = 12;
                            //第二轮预选赛已淘汰(未能晋级冠军赛)   
                            $clubStatusNew['status'] == 10 && $homeInfo['clubStatusNew'] = 13;    
                        }
                        if($clubStatusNew['type'] == 15){    //第二轮决赛
                            //第二轮冠军赛进行中   
                            $clubStatusNew['status'] == 15 && $homeInfo['clubStatusNew'] = 14;
                            //第二轮冠军赛空档期   
                            $clubStatusNew['status'] == 20 && $homeInfo['clubStatusNew'] = 15;
                            //第二轮冠军赛已淘汰(未能晋级争霸赛)   
                            $clubStatusNew['status'] == 10 && $homeInfo['clubStatusNew'] = 16;   
                        }
                        if($clubStatusNew['type'] == 20){    //第三轮预选赛
                            //第三轮预选赛招募期(当前战队无状态)
                            $clubStatusNew['status'] == 0 && $homeInfo['clubStatusNew'] = 17; 
                            //第三轮预选赛已淘汰(人数未满下限)   
                            $clubStatusNew['status'] == 5 && $homeInfo['clubStatusNew'] = 18;  
                            //第三轮预选赛进行中   
                            $clubStatusNew['status'] == 15 && $homeInfo['clubStatusNew'] = 19;
                            //第三轮预选赛空档期   
                            $clubStatusNew['status'] == 20 && $homeInfo['clubStatusNew'] = 20;
                            //第三轮预选赛已淘汰(未能晋级冠军赛)   
                            $clubStatusNew['status'] == 10 && $homeInfo['clubStatusNew'] = 21;    
                        }
                        if($clubStatusNew['type'] == 25){    //第三轮决赛
                            //第三轮冠军赛进行中   
                            $clubStatusNew['status'] == 15 && $homeInfo['clubStatusNew'] = 22;
                            //第三轮冠军赛空档期   
                            $clubStatusNew['status'] == 20 && $homeInfo['clubStatusNew'] = 23;
                            //第三轮冠军赛已淘汰(未能晋级争霸赛)   
                            $clubStatusNew['status'] == 10 && $homeInfo['clubStatusNew'] = 24;   
                        }
                        if($clubStatusNew['type'] == 100){    //争霸赛  
                            //争霸赛进行中   
                            $clubStatusNew['status'] == 15 && $homeInfo['clubStatusNew'] = 25;
                            //争霸赛空档期   
                            $clubStatusNew['status'] == 20 && $homeInfo['clubStatusNew'] = 26;  
                        }
                    }
                    //查看俱乐部是否具有争霸赛资格
                    $clubChampions = $this->load('paome/home')->clubChampions($season['id'], $clubInfo['id']);
                    $clubInfo['isChampions'] = 0;   //是否晋级争霸赛
                    !empty($clubChampions) && $clubInfo['isChampions'] = 1;

                    //争霸赛结束后获取战队积分排名
                    $scoreSql = "select score_rank_num from r_pm_score where contest_id = {$season['id']} and club_id = {$clubInfo['id']} and type = 100";
                    $clubInfo['scoreRank'] = $this->db->getOne($scoreSql);
                }
                $this->result['result']['clubInfo'] = $clubInfo;
            }
        }

        if($homeInfo['isJoin'] != 2){
            /*$homeInfo['isJoin'] == 0 && $clubId = null;
            $homeInfo['isJoin'] == 1 && $clubId = $userClubStatus['club_id'];*/
            //俱乐部推荐
            $recommentClub = $this->load('paome/home')->HomeClubRecomment($uid);
            //获取团长的头像
            if(!empty($recommentClub)){
                foreach ($recommentClub as &$v) {
                    $v['leader_head'] = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $v['head_uid'], 'header');
                    $v['leader_name'] = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $v['head_uid'], 'nickname');
                }
           }
           $this->result['result']['recommentClub'] = $recommentClub;
        }

        //获取当前所处阶段及对应时间范围
        $timeRang = $this->load("paome/home")->getTimeRang();
        if(!empty($timeRang)){
            $start_time = $timeRang['start_time'];
            $end_time = $timeRang['end_time'];
            if(date('Y', $start_time) == date('Y', $end_time)){
                $timeRang['start_time'] = date('m.d', $start_time);
                $timeRang['end_time'] = date('m.d', $end_time);
                $timeRang['start_seconds'] = date('m.d H:i', $start_time);
                $timeRang['end_seconds'] = date('m.d H:i', $end_time);
            }else{
                $timeRang['start_time'] = date('Y.m.d', $start_time);
                $timeRang['end_time'] = date('Y.m.d', $end_time);
                $timeRang['start_seconds'] = date('Y.m.d H:i', $start_time);
                $timeRang['end_seconds'] = date('Y.m.d H:i', $end_time);
            }
            if(date('Y', $timeRang['qualifying_start']) == date('Y', $timeRang['qualifying_end'])){
                $timeRang['qualifying_start'] = date('m.d H:i', $timeRang['qualifying_start']);
                $timeRang['qualifying_end'] = date('m.d H:i', $timeRang['qualifying_end']);
            }else{
                $timeRang['qualifying_start'] = date('Y.m.d H:i', $timeRang['qualifying_start']);
                $timeRang['qualifying_end'] = date('Y.m.d H:i', $timeRang['qualifying_end']);
            }
            if(date('Y', $timeRang['championship_start']) == date('Y', $timeRang['championship_end'])){
                $timeRang['championship_start'] = date('m.d H:i', $timeRang['championship_start']);
                $timeRang['championship_end'] = date('m.d H:i', $timeRang['championship_end']);
            }else{
                $timeRang['championship_start'] = date('Y.m.d H:i', $timeRang['championship_start']);
                $timeRang['championship_end'] = date('Y.m.d H:i', $timeRang['championship_end']);
            }
        }
        $wxUrl = "http://".$this->config['host']."/paome_webIndex/paoshare";
        $shareTitle = "我正在参与跑么杯争霸赛，快来加入我的战队赢取5000元奖励吧！";
        $description = "11月14日，就在虎扑跑步";
        if($homeInfo['isJoin'] == 2){
            //$wxUrl = $this->load('paome/share')->pubOpenIdUrl($uid);
            $wxUrl = "http://".$this->config['host']."/paome_share/shareLink?target_uid=".$uid;
            $shareTitle = "我正在参与跑么杯争霸赛，需要你的助力！助力即可抽奖！";
            $description = "每人每天可抽奖三次，最高可赢得iPhone7";
        }
        $this->result['result']['wxUrl'] = $wxUrl;
        $this->result['result']['shareTitle'] = $shareTitle;
        $this->result['result']['description'] = $description;
        $this->result['result']['timeRang'] = $timeRang;
        $this->result['result']['homeInfo'] = $homeInfo;
    }

    /**
     * 获取首页排行榜数据
     */
    public function dohomeClubRank() {
        $this->printResult = TRUE;
        $uid = $this->uid;
        if (!$uid) {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $result = array('total'=>0,'list'=>[]);
        //查询当前的赛季
        $paomeInfo = $this->load('paome/cup')->getCurrentPaomeInfo();
        if (!$paomeInfo) {
            $this->result['result']= $result;
            exit;
        }
        $paomeId = $paomeInfo['id'];
        $roundId = (int)common::request('roundId', 'G', 1, true);
        $typeWd = (int)common::request('typeWd', 'G', 1, true);
        $clubName = (int)common::request('clubName', 'G', '', true);
        $rankModel = $this->load('paome/rank');
        $list = $rankModel->getRankForH5($paomeId,$roundId,$typeWd,$uid,$clubName);
        $this->result['result'] = array('total'=>$rankModel->total,'list'=>$list);
    }

    /**
     * 争霸赛期间首页战队数据
     */
    public function doClubDataInZb() {
        $this->printResult = TRUE;
        $uid = $this->uid;
        if (!$uid) {
            $_ENV['error']->getErrors(201);
            exit;
        }
        //查询当前的赛季
        $paomeInfo = $this->load('paome/cup')->getCurrentPaomeInfo();
        if (!$paomeInfo) {
            $this->result['result']['clubData'] = array();
            exit;
        }
        $round = $this->load('paome/paomeHall')->getCurrentRound();
        //获取用户所属战队
        $clubInfo = $this->load("paome/paomeHall")->getClubByUid($uid);
        $isJoin = 0;
        $clubData = array();
        if($clubInfo){
            $isJoin = 1;
            /*$clubData = $this->load('paome/center')->getCurrentClubRank($clubInfo['id'], $paomeInfo);
            if($clubData){
                $clubData['mileage_score'] = $this->load('paome/rank')->getClubRankScore($clubData['club_mileage_rank'], 1);
                $clubData['speed_score'] = $this->load('paome/rank')->getClubRankScore($clubData['club_speed_rank'], 5);
                $clubData['support_score'] = $this->load('paome/rank')->getClubRankScore($clubData['club_supporters_rank'], 10);
                $clubData['today_score'] = $clubData['mileage_score'] + $clubData['speed_score'] + $clubData['support_score'];
                $totalScore = $this->load('paome/home')->getClubSumScore($clubInfo['id']);
                $clubData['total_score'] = isset($totalScore['total_score']) ? $totalScore['total_score'] : '今日24点更新';
            }*/
            //获取战队各个维度的排名和数据
            $date = date('Ymd');
            //里程
            $clubData['club_mileage_rank'] = $this->load('paome/rank')->getObjectDayRank($clubInfo['id'], $paomeInfo['id'], $round, $date, 1);   //排名
            $clubData['club_mileage'] = $this->load('paome/rank')->getObjectDayRankValue($clubInfo['id'], $paomeInfo['id'], $round, $date, 1);   //数据
            $clubData['mileage_score'] = $this->load('paome/rank')->getClubRankScore($clubData['club_mileage_rank'], 1);    //积分

            //配速
            $clubData['club_speed_rank'] = $this->load('paome/rank')->getObjectDayRank($clubInfo['id'], $paomeInfo['id'], $round, $date, 5);   //排名
            $clubData['club_speed'] = $this->load('paome/rank')->getObjectDayRankValue($clubInfo['id'], $paomeInfo['id'], $round, $date, 5);   //数据
            $clubData['speed_score'] = $this->load('paome/rank')->getClubRankScore($clubData['club_speed_rank'], 5);    //积分

            //人气
            $clubData['club_supporters_rank'] = $this->load('paome/rank')->getObjectDayRank($clubInfo['id'], $paomeInfo['id'], $round, $date, 10);   //排名
            $clubData['club_supporters'] = $this->load('paome/rank')->getObjectDayRankValue($clubInfo['id'], $paomeInfo['id'], $round, $date, 10);   //数据
            $clubData['support_score'] = $this->load('paome/rank')->getClubRankScore($clubData['club_supporters_rank'], 10);    //积分

            $clubData['today_score'] = $clubData['mileage_score'] + $clubData['speed_score'] + $clubData['support_score'];
            $totalScore = $this->load('paome/home')->getClubSumScore($clubInfo['id']);
            $clubData['total_score'] = isset($totalScore['total_score']) ? $totalScore['total_score'] : '今日24点更新';

            $clubData['club_id'] = $clubInfo['id'];
            $clubData['club_name'] = $clubInfo['title'];
            $clubData['club_header'] = $clubInfo['image'];
        }
        
        $this->result['result']['clubData'] = $clubData;
    }

    /**
     * 首页获取明星成员数据
     * @return [type] [description]
     */
    public function dohomeStarUser(){
        $uid = $this->uid;
        if(empty($uid))
        {
          $_ENV['error']->getErrors(201);
          exit;
        }
        $order = $this->redis->get(REDIS_PREFIX . 'HomestarOrder' . $uid);
        $order = $order ? $order : 'max_mileage';
        $sort = 'DESC';
        $starUser = array();    //明星成员
        if($order == 'max_mileage'){
            $starUser = $this->load("paome/home")->getHomeStarUser($order, $sort);
            if($starUser){
                empty($starUser['max_mileage']) && $starUser = array();
            }
            $this->redis->set(REDIS_PREFIX . 'HomestarOrder' . $uid, 'speed');    
        }
        if ($order == 'speed') {
            $starUser = $this->load("paome/home")->getHomeStarUser($order, 'ASC');
            if($starUser){
                empty($starUser['speed']) && $starUser = array();
            }
            $this->redis->set(REDIS_PREFIX . 'HomestarOrder' . $uid, 'support_num');
        }
        if ($order == 'support_num') {
            $starUser = $this->load("paome/home")->getHomeStarUser($order, $sort);
            if($starUser){
                empty($starUser['support_num']) && $starUser = array();
            }
            $this->redis->set(REDIS_PREFIX . 'HomestarOrder' . $uid, 'mileage');
        }
        if ($order == 'mileage') {
            $starUser = $this->load("paome/home")->getHomeStarUser($order, $sort);
            if($starUser){
                empty($starUser['mileage']) && $starUser = array();
            }
            $this->redis->set(REDIS_PREFIX . 'HomestarOrder' . $uid, 'max_mileage');
        }
        if(empty($starUser)){
            $order = 'max_mileage';
            $starUser = $this->load("paome/home")->getHomeStarUser($order, $sort); 
            if($starUser){
                empty($starUser['max_mileage']) && $starUser = array();
            }
        }
        if(empty($starUser)){
            $order = 'speed';
            $starUser = $this->load("paome/home")->getHomeStarUser($order, 'ASC'); 
            if($starUser){
                empty($starUser['speed']) && $starUser = array();
            }
        }
        if(empty($starUser)){
            $order = 'support_num';
            $starUser = $this->load("paome/home")->getHomeStarUser($order, $sort); 
            if($starUser){
                empty($starUser['support_num']) && $starUser = array();
            }
        }
        if(empty($starUser)){
            $order = 'mileage';
            $starUser = $this->load("paome/home")->getHomeStarUser($order, $sort); 
            if($starUser){
                empty($starUser['mileage']) && $starUser = array();
            }
        }

        if($starUser){
            //用户具体信息
            $userInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $starUser['uid']);
            $starUser['nickname'] = '';
            $starUser['header'] = '';
            if($userInfo){
                $starUser['nickname'] = $userInfo['nickname'];
                $starUser['header'] = $userInfo['header'];
            }
            $starUser['label'] = $order;  //明星标签
            if($order == 'speed' && !empty($starUser['speed'])){
             $starUser['speed'] = common::sec3format($starUser['speed']);
            }
            //用户所属俱乐部信息
            $clubInfo = $this->load("paome/paomeHall")->getClubByUid($starUser['uid']);
            if(!empty($clubInfo)){
                $starUser['club_name'] = $clubInfo['title'];
                $starUser['club_header'] = $clubInfo['image'];
            }
            
        } 
        $this->result['result']['starUser'] = $starUser;  
    }
    
    /**
     * 跑么杯大厅公告
     * @return [type] [description]
     */
    public function dohallAffiche(){
        $uid = $this->uid ? $this->uid : 0;
        //获取当前赛季状态
        $season = $this->load('paome/paomeHall')->getCurrentSeason();
        $round = $this->load("paome/paomeHall")->getPhaseByTime(time(), time());
        $affiche = array();
        $affiche['content'] = "猜猜哪个战队能在跑么杯竞赛中拔得头筹！快去社区话题发表你的观点吧！";
        //跑量key
        $rankClubKeyMile = 'rankClub-mileage-'.$season['id'].'-'.$season['status']; 
        //配速key
        $rankClubKeySpeed = 'rankClub-speed-'.$season['id'].'-'.$season['status'];
        //人气key
        $rankClubKeySupportNum = 'rankCLub-supportNum-'.$season['id'].'-'.$season['status'];
        //全员跑量key
        $rankUserKeyMile = 'rankUser-mileage-'.$season['id'].'-'.$season['status'];
        if(!empty($round)){
            if($round['type'] == 1){   //招募期
                $index = $this->redis->get(REDIS_PREFIX . 'RecruitIndex' .$uid);
                empty($index) && $index = 1;
                if($index == 1){
                    //创建战队公告
                    $where = "status = 0 and user_num < 30";
                    $order = "create_time desc";
                    $latestClub = $this->load("paome/home")->latestClub($where, $order);
                    if(!empty($latestClub)){
                        //获取创建人信息
                        $nickname = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $latestClub['head_uid'], 'nickname');
                        $affiche['content'] = $nickname."创建了".$latestClub['title']."战队，快快来加入吧";
                    }
                    $index++;
                }elseif($index == 2){
                    //战队还差一人公告
                    $where = "status = 0 and user_num = 4";
                    $order = "create_time desc";
                    $latestClub = $this->load("paome/home")->latestClub($where, $order);
                    if(!empty($latestClub)){
                        $affiche['content'] = $latestClub['title']."战队再招募一个人就可以竞赛咯，快快加入吧";
                    }
                    $index++;
                }elseif($index == 3){
                    //大于5人小于30人公告
                    $where = "status = 0 and user_num > 5 and user_num < 30";
                    $order = "create_time desc";
                    $latestClub = $this->load("paome/home")->latestClub($where, $order);
                    if(!empty($latestClub)){
                        $affiche['content'] = $latestClub['title']."战队已招募".$latestClub['user_num']."成员，战队成员都是大神哦，快快加入战队吧";
                    }
                    $diff = $round['end_time'] - time();
                    if(ceil($diff/86400) == 1){
                       $index++; 
                    }else{
                        $index = 1;
                    }  
                }   
                if($index == 4 || empty($affiche['content'])){
                    $affiche['content'] = "第一轮竞赛预选赛即将开始，各位战队做好备战计划进击跑量榜，配速榜和人气榜吧";
                    $index = 1;
                }
                $this->redis->set(REDIS_PREFIX . 'RecruitIndex' .$uid, $index);
            }elseif(in_array($round['type'], array(5, 15, 25))){   //预选赛
                $index = $this->redis->get(REDIS_PREFIX . 'QualifyingIndex' .$uid);
                empty($index) && $index = 1;
                if($index == 1){
                    //配速榜第一被更新
                    $clubSpeed = $this->redis->zRange($rankClubKeySpeed,0,0);
                    if($clubSpeed){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubSpeed[0]}");
                        $affiche['content'] = $clubName."配速目前超越了所有战队，今日能稳拿第一么！各位猜猜看";
                    }
                    $index++;
                }elseif($index == 2){
                    //跑量榜第一被更新
                    $clubMileage = $this->redis->zRange($rankClubKeyMile,0,0);
                    if($clubMileage){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubMileage[0]}");
                        $affiche['content'] = $clubName."跑量目前超越了所有战队，今日能稳拿第一么！各位猜猜看";
                    }
                    $index++;
                }elseif($index == 3){
                    //人气榜第一被更新
                    $clubSupport = $this->redis->zRange($rankClubKeySupportNum,0,0);
                    if($clubSupport){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubSupport[0]}");
                        $affiche['content'] = $clubName."人气目前超越了所有战队，今日能稳拿第一么！各位猜猜看";
                    }
                    $index++;
                }elseif($index == 4){
                    //全员跑量榜第一
                    $userMileage = $this->redis->zRange($rankUserKeyMile,0,0);
                    if($userMileage){
                        $userName = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $userMileage[0], 'nickname');
                        $affiche['content'] = $userName."目前是全员跑量榜的第一名哦！猜猜看他会不会被超越！";
                    }
                    $index++;
                }elseif($index == 5){
                    //预选赛开始三天内
                    $diff = time() - $round['start_time'];
                    if(ceil($diff/86400) <= 3){
                        $affiche['content'] = "跑么杯预选赛开始，看看哪个战队能拔得头筹！各位猜一猜！";
                    }
                    $index++;
                }elseif($index == 6){
                    //预选赛最后一天
                    $diff = $round['end_time'] - time();
                    if(ceil($diff/86400) == 1){
                        $affiche['content'] = "预选赛今日22:00点结束请各位在结束前上传自己的成绩不然成绩不做统计哦！";
                    }
                    $index++;
                }
                if($index == 7 || empty($affiche['content'])){
                    $affiche['content'] = "预选赛晋级冠军赛的条件是在跑量榜上排名前70%哦！大家加油啊！";
                    $index = 1;
                }
                $this->redis->set(REDIS_PREFIX . 'QualifyingIndex' .$uid, $index);
            }elseif(in_array($round['type'], array(6, 16, 26))){    //预选赛22:00至冠军赛开始前
                $affiche['content'] = "晋级至冠军赛的战队已经发送邀请喽！没有晋级的战队别气馁还可以继续第二轮哦！";
            }elseif(in_array($round['type'], array(10, 20, 30))){      //冠军赛
                $index = $this->redis->get(REDIS_PREFIX . 'ChampionshipIndex' .$uid);
                empty($index) && $index = 1;
                if($index == 1){
                    $diff = time() - $round['start_time'];
                    if(ceil($diff/86400) == 1){
                        $affiche['content'] = "冠军赛今日2点开始喽！快快摆好姿势跑起来吧！";
                    }
                    $index++;
                }elseif($index == 2){
                    //跑量榜第一被更新
                    $clubMileage = $this->redis->zRange($rankClubKeyMile,0,0);
                    if($clubMileage){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubMileage[0]}");
                        $affiche['content'] = $clubName."打破了跑量榜第一名的记录，猴赛雷呀！";
                    }
                    $index++;
                }elseif($index == 3){
                    //配速榜第一被更新
                    $clubSpeed = $this->redis->zRange($rankClubKeySpeed,0,0);
                    if($clubSpeed){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubSpeed[0]}");
                        $affiche['content'] = $clubName."打破了配速榜第一名的记录，猴赛雷呀！";
                    }
                    $index++;
                }elseif($index == 4){
                     //人气榜第一被更新
                    $clubSupport = $this->redis->zRange($rankClubKeySupportNum,0,0);
                    if($clubSupport){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubSupport[0]}");
                        $affiche['content'] = $clubName."打破了人气榜第一名的记录，猴赛雷呀！";
                    }
                    $index++;
                }elseif($index == 5){
                    //全员跑量榜第一
                    $userMileage = $this->redis->zRange($rankUserKeyMile,0,0);
                    if($userMileage){
                        $userName = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $userMileage[0], 'nickname');
                        $affiche['content'] = $userName."的目前的累计跑量超越了所有参与者，谁去挑战他！";
                    }
                    $index++;
                }elseif($index == 6){
                    $affiche['content'] = "第一轮决赛开始啦！看看你喜欢的战队上榜前10了么！";
                    $index++;
                }elseif($index == 7){
                    $affiche['content'] = "跑量榜前10名已经出来啦！你们上榜了么！他们会不会被超越？猜猜看吧！";
                    $index++;
                }elseif($index == 8){
                     $diff = $round['end_time'] - time();
                    if(ceil($diff/86400) == 1){
                        $affiche['content'] = "预选赛今日22：00点结束请各位参赛者在结束前上传自己的成绩不然成绩不做统计哦！";
                    }
                    $index++;
                }
                if($index == 9 || empty($affiche['content'])){
                    $affiche['content'] = "冠军赛结束时跑量榜排名前的10的战队可以直接获得总决赛冠军争霸赛的资格，大家加油啊！";
                    $index = 1;
                }
                $this->redis->set(REDIS_PREFIX . 'ChampionshipIndex' .$uid, $index);
            }elseif(in_array($round['type'], array(31, 32))){    //冠军赛至争霸赛空挡
                $index = $this->redis->get(REDIS_PREFIX . 'AfterThirdIndex' .$uid);
                empty($index) && $index = 1;
                if($index == 1){
                    $affiche['content'] = "获得总决赛冠军争霸赛的战队已给收到直通名额卡喽！恭喜恭喜！备战总决赛赢得三冠王吧！";
                    $index++;
                }elseif($index == 2){
                     $affiche['content'] = "期待已久的总决赛冠军争霸赛终于来了！跑量榜！配速榜！人气榜！各榜榜首花落谁家？大家快去猜猜看";
                    $index = 1;
                }
                $this->redis->set(REDIS_PREFIX . 'AfterThirdIndex' .$uid, $index);
            }elseif($round['type'] == 105){    //争霸赛
                $index = $this->redis->get(REDIS_PREFIX . 'ChampionsIndex' .$uid);
                empty($index) && $index = 1;
                if($index == 1){
                    //跑量榜第一被更新
                    $clubMileage = $this->redis->zRange($rankClubKeyMile,0,0);
                    if($clubMileage){
                        $affiche['content'] = "今日跑量榜第一名已经产生喽！会不会被超越呢！大家加油吧！";
                    }
                    $index++;
                }elseif($index == 2){
                    //配速榜第一被更新
                    $clubSpeed = $this->redis->zRange($rankClubKeySpeed,0,0);
                    if($clubSpeed){
                        $affiche['content'] = "今日配速榜第一名已经产生喽！会不会被超越呢！大家加油吧！";
                    }
                    $index++;
                }elseif($index == 3){
                     //人气榜第一被更新
                    $clubSupport = $this->redis->zRange($rankClubKeySupportNum,0,0);
                    if($clubSupport){
                        $affiche['content'] = "今日人气榜第一名已经产生喽！会不会被超越呢！大家加油吧！";
                    }
                    $index++;
                }elseif($index == 4){
                    //跑量榜第一被更新
                    $clubMileage = $this->redis->zRange($rankClubKeyMile,0,0);
                    if($clubMileage){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubMileage[0]}");
                        $affiche['content'] = $clubName."打破了跑量榜 实力好强大呀！";
                    }
                    $index++;
                }elseif($index == 5){
                    //配速榜第一被更新
                    $clubSpeed = $this->redis->zRange($rankClubKeySpeed,0,0);
                    if($clubSpeed){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubSpeed[0]}");
                        $affiche['content'] = $clubName."打破了配速榜 实力好强大呀！";
                    }
                    $index++;
                }elseif($index == 6){
                     //人气榜第一被更新
                    $clubSupport = $this->redis->zRange($rankClubKeySupportNum,0,0);
                    if($clubSupport){
                        $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubSupport[0]}");
                        $affiche['content'] = $clubName."打破了人气榜 实力好强大呀！";
                    }
                    $index++;
                }elseif($index == 7){
                    $affiche['content'] = "跑量榜！配速榜！人气榜三榜各有特色！都是体现团队协作精神与团长的作战指挥能力的哦！荣誉一样！擅长哪个榜单就齐心协力一定能取得好成绩！";
                    $index++;
                }elseif($index == 8){
                    $diff = $round['end_time'] - time();
                    if(ceil($diff/86400) == 1){
                        $affiche['content'] = "争霸赛今日24:00点结束，请各位参赛者在结束前上传自己的成绩不然成绩不做统计哦！";
                    }
                    $index++;
                }
                if($index == 9 || empty($affiche['content'])){
                    $affiche['content'] = "跑量榜，配速榜，人气榜各榜单前10名都会获得丰厚大礼哦，大家加油吧！";
                    $index = 1;
                }
                $this->redis->set(REDIS_PREFIX . 'ChampionsIndex' .$uid, $index);
            }elseif($round['type'] == 106){     //争霸赛结束
                $affiche['content'] = "争霸赛今日24:00点结束，请各位参赛者在结束前上传自己的成绩不然成绩不做统计哦！";
            }elseif(in_array($round['type'], array(11, 12, 21, 22))){   //常规休赛期
                $index = $this->redis->get(REDIS_PREFIX . 'OffseasonIndex' .$uid);
                empty($index) && $index = 1;
                if($index == 1){
                    $affiche['content'] = "现在是休赛期间，各战队要休养生息，招兵买马为下场竞赛做好准备呀！";
                    $index++;
                }elseif($index == 2){
                    $affiche['content'] = "希望大家踊跃提出自己宝贵的建议，让跑么杯更好玩！";
                    $index = 1;
                }
                $this->redis->set(REDIS_PREFIX . 'OffseasonIndex' .$uid, $index);
            }   
            $gameRound = 0;
            $round['type'] == 1 && $gameRound = 1;
            in_array($round['type'], array(5, 15, 25)) && $gameRound = 2;
            in_array($round['type'], array(6, 16, 26)) && $gameRound = 3;
            in_array($round['type'], array(10, 20, 30)) && $gameRound = 4;
            in_array($round['type'], array(31, 32)) && $gameRound = 5;
            $round['type'] == 105 && $gameRound = 6;
            $round['type'] == 105 && $gameRound = 7;
            in_array($round['type'], array(11, 12, 21, 22)) && $gameRound = 8;
            if($gameRound){
                //获取公告
                $hallAffiche = $this->load('paome/home')->getAffiche($round['contest_id'], $gameRound, $affiche['content'], 1);
                if(empty($hallAffiche)){
                    $affiche['add_time'] = time();
                    $affiche['contest_id'] = $round['contest_id'];
                    $affiche['rounds'] = $gameRound;
                    $affiche['type'] = 1;
                    $this->db->insertRowTable('r_pm_affiche', $affiche);
                }
            }            
        }
        if($season['status'] == 120){
            $index = $this->redis->get(REDIS_PREFIX . 'ChampionsEndIndex' .$uid);
            empty($index) && $index = 1;
            if($index == 1){
                //积分榜第一被更新
                $clubId = $this->db->getOne("select club_id from r_pm_score where contest_id={$season['id']} and score_rank_num = 1 limit 1");
                if($clubId){
                    $clubName = $this->db->getOne("select title from r_pm_club where status = 0 and id = {$clubId}");
                    $affiche['content'] = "恭喜".$clubName."获得了跑么杯总决赛的积分冠军！ 获得了5000元的丰厚大礼包及奖牌，奖杯！祝贺你们！";
                }
                $index++;
            }
            if($index == 2 || empty($affiche['content'])){
                $affiche['content'] = "感谢各位参与跑么杯第一赛季！以及各位的宝贵建议！第二赛季即将开始尽请期待！";
                $index = 1;
            } 
            $this->redis->set(REDIS_PREFIX . 'ChampionsEndIndex' .$uid, $index);
        }
        $this->result['result']['content'] = $affiche['content'];
    }

    /**
     * 跑么杯大厅公告列表
     * @return [type] [description]
     */
    public function dohallAfficheList(){
        $p = common::request('p', 'R', 1);   //页码
        $countSql = "select count(1) from r_pm_affiche where is_show = 1 and type = 1";
        $offset = 20;
        $num = $this->db->getOne($countSql);

        //分页
        require FILE . 'lib/page.class.php';
        $page = new Page($num, $offset);
        $pageStr = $page->show();
        $start = ($p - 1) * $offset;

        $list = $this->db->getAll("select * from r_pm_affiche where is_show = 1 and type = 1 order by add_time desc limit $start,$offset");
        if($list){
            foreach ($list as &$v) {
                $diffTime = time() - $v['add_time'];
                if($diffTime <= 604800){
                    $v['add_time'] = common::timediff($diffTime, 'paome');
                }else{
                    if(date('Y', $v['add_time']) == date('Y')){
                        $v['add_time'] = date('m-d H:i');
                    }else{
                        $v['add_time'] = date('Y-m-d H:i');
                    }
                }
            }
        }
        $this->result['result']['list'] = $list;
    }
}